package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author 喵喵喵
 * Date 2025/6/7 9:00
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<SetmealVO> setmealPage=setmealMapper.pageSetmeal(dto);
        long total = setmealPage.getTotal();
        List<SetmealVO> setmealList = setmealPage.getResult();

        return new PageResult(total,setmealList);
    }

    @Transactional
    @Override
    public void addSetmeal(SetmealDTO dto) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.addSetmeal(setmeal);

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish sd: setmealDishes) {
                sd.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.addSetmealDish(setmealDishes);
        }
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        List<SetmealDish> setmealDish = setmealDishMapper.getSetmealDishById(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;

    }


    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);//dto传入setmeal
        setmealMapper.updateSetmeal(setmeal);//修改
        //2.
        setmealDishMapper.deleteBySetmealId(setmeal.getId());//删除setmeal_dish
        List<SetmealDish> dishes=setmealDTO.getSetmealDishes();//获取dish集合
        if (dishes != null && !dishes.isEmpty()) {
            for (SetmealDish sd : dishes) {
                sd.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.addSetmealDish(dishes);
        }

    }

    @Override
    public void StartORStopStatus(Integer status, Integer id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id.longValue())
                /*.updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    public void deleteSetmealByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal=setmealMapper.getSetmealById(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //2.删除操作
        for (Long id : ids) {
            setmealMapper.deleteSetmealById(id);
            setmealDishMapper.deleteBySetmealId(id);

        }
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
